package net.core.base.struts;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.core.base.bean.Pagination;
import net.core.base.json.JSONParam;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport {

	private static final long serialVersionUID = -171374347857564643L;

	private String backURL;
	protected String errorMsg;
	
	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	public HttpSession getSession() {
		return getRequest().getSession();
	}
	
	public String getPath() {
		return getRequest().getContextPath();
	}
	/**
	 * 提供获取参数的方法
	 * @param paramname
	 * @return Object
	 */
	public Object getParam(String paramname){
		if(paramname==null){
			return "";
		}
		return getRequest().getParameter(paramname);
		
	}	
	/**
	 * 设置查询参数
	 * @param page
	 * @param strpage
	 */
	public void setPageParameters(Pagination page,String strpage){
		Map param = ActionContext.getContext().getParameters();
		
		if(getSelectPerPage(strpage)==-1){
		}else{
			page.setPageRecord(getSelectPerPage(strpage));
		}		
		StringBuffer hidden = new StringBuffer();
		
		Iterator paramIt = param.keySet().iterator();
		
		//String paramStr = "";
		while (paramIt.hasNext()){
			String paramName = (String)paramIt.next();
			String v[] = (String[])param.get(paramName);
			if(paramName.equals("page_currentPage_"+strpage)){
				try{
					if(Integer.parseInt(v[0])>page.getTotalPage()){
						page.setCurrentPage(page.getTotalPage());
					}else{
						page.setCurrentPage(Integer.parseInt(v[0]));
					}					
					continue;
				}catch(Exception ex){
					page.setCurrentPage(1);
					continue;
				}
			}
			if(paramName.equals("page_queryflag_"+strpage)){
				continue;
			}
			if(paramName.toLowerCase().equals("submit")){
				continue;
			}
			
			//将参数转为隐含域,用于翻页进使用.
			if(!paramName.equals("page_currentPage_"+strpage)
			   &&!paramName.equals("page_selectPerPage_"+strpage)
			   &&!paramName.equals("totalPage"+strpage)
			   &&!paramName.equals("queryStringInfo")){
				for(int i=0;i<v.length;i++){
					String value = freemarker.template.utility.StringUtil.HTMLEnc(v[i]);
					hidden.append("<Input Type=\"hidden\" id=\""+paramName+"\" name=\""+paramName+"\" value=\""+ value + "\">");
				}
			}
		}
		page.setCondition(hidden.toString());
	}
	
	/**
	 * 判断是否设置了每页显示多少条记录
	 * @param page
	 * @return int
	 */
	public int getSelectPerPage(String page){
		if(getParam("page_selectPerPage_"+page)!=null){
			try{
				return Integer.parseInt(getParam("page_selectPerPage_"+page).toString().replace(",", ""));
			}catch(Exception ex){
				return -1;
			}
		}else{
			return -1;
		}
	}
	
	protected HashMap<String, String> convertToMap(JSONParam[] params) {
		HashMap<String, String> map = new HashMap<String, String>();
		if (params != null) {
			for (JSONParam param : params) {
				map.put(param.getName(), param.getValue());
			}
		}
		return map;
	}

	public String getBackURL() {
		return backURL;
	}

	public void setBackURL(String backURL) {
		this.backURL = backURL;
	}

	public String getErrorMsg() {
		return errorMsg;
	}

	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}	
	
}